from app.Dao.TodoDao.TodoDao import TodoDAO  # 对应你的TodoDAO路径
from typing import List, Dict, Optional
from datetime import date, datetime, timedelta


class TodoService:
    """Todo清单Service层（处理业务逻辑，转发DAO操作）"""

    @staticmethod
    def add_todo(
            user_id: int,
            description: str,
            deadline: date = date.today(),
            goal_id: int = None,
            self_score: int = 5,
            importance: int = 1,  # 重要不紧急 默认
            remark: str = None,
            is_done: int = 0,  # 0待完成
            project_id: Optional[int] = None,
            attachment: Optional[str] = None,
            focus_time: int = 0
    ) -> (bool, str):
        """
        新增Todo任务（业务层：参数合法性校验 + 转发DAO）
        :param 同TodoDAO.add_todo
        :return: (bool, 提示字符串)
        """

        # 2. 转发DAO层新增操作
        return TodoDAO.add_todo(
            user_id=user_id,
            description=description,
            deadline=deadline,
            goal_id=goal_id,
            self_score=self_score,
            importance=importance,
            remark=remark,
            is_done=is_done,
            project_id=project_id,
            attachment=attachment,
            focus_time=focus_time
        )

    @staticmethod
    def delete_todo(todo_id: int) -> (bool, str):
        """
        删除Todo任务（业务层：权限校验预留 + 转发DAO）
        :param todo_id: 任务ID
        :return: (bool, 提示字符串)
        """
        # 1. 业务层权限校验（示例：可扩展为「仅任务创建者/管理员可删除」）
        # 此处预留权限校验逻辑，例如：
        # success, todo_info = TodoDAO.get_todo_by_id(todo_id)
        # if not success:
        #     return False, todo_info
        # if todo_info["user_id"] != current_user_id:  # current_user_id需从上下文获取（如登录态）
        #     return False, f"无权限删除他人任务（任务ID：{todo_id}）"

        # 2. 转发DAO层删除操作
        return TodoDAO.delete_todo(todo_id=todo_id)

    @staticmethod
    def update_todo(
            todo_id: int,
            description: Optional[str] = None,
            deadline: Optional[date] = None,
            goal_id: Optional[int] = None,
            project_id: Optional[int] = None,
            self_score: Optional[int] = None,
            importance: Optional[int] = None,
            is_done: Optional[int] = None,
            remark: Optional[str] = None,
            attachment: Optional[str] = None,
            focus_time: Optional[int] = None
    ) -> (bool, str):
        """
        更新Todo任务（业务层：参数合法性校验 + 状态变更规则 + 转发DAO）
        :param 同TodoDAO.update_todo
        :return: (bool, 提示字符串)
        """

        # 3. 转发DAO层更新操作
        return TodoDAO.update_todo(
            todo_id=todo_id,
            description=description,
            deadline=deadline,
            goal_id=goal_id,
            project_id=project_id,
            self_score=self_score,
            importance=importance,
            is_done=is_done,
            remark=remark,
            attachment=attachment,
            focus_time=focus_time
        )

    @staticmethod
    def get_todo_by_id(todo_id: int):
        """
        根据ID查询单个Todo任务（业务层：权限校验 + 转发DAO）
        :param todo_id: 任务ID
        :return: (bool, 结果字典/提示字符串)
        """
        # 1. 业务层权限校验（示例：仅任务创建者/管理员可查看）
        # success, todo_info = TodoDAO.get_todo_by_id(todo_id)
        # if not success:
        #     return False, todo_info
        # if todo_info["user_id"] != current_user_id and not current_user_is_admin:
        #     return False, f"无权限查看他人任务（任务ID：{todo_id}）"

        # 2. 转发DAO层查询操作（若上面做了权限校验，此处可直接返回todo_info，避免重复查询）
        return TodoDAO.get_todo_by_id(todo_id=todo_id)

    @staticmethod
    def get_all_todos(
            user_id: Optional[int] = None,
            is_done: Optional[int] = None,
            goal_id: Optional[int] = None,
            deadline: str = None
    ):
        """
        查询Todo任务列表（业务层：筛选条件扩展 + 权限过滤 + 转发DAO）
        :param user_id: 筛选用户ID（可选）
        :param is_done: 筛选完成状态（0/1，可选）
        :param goal_id: 筛选目标ID（可选）
        :param deadline: 任务截止时间
        :return: (bool, 结果列表/提示字符串)
        """

        # 3. 转发DAO层查询操作
        success, result = TodoDAO.get_all_todos(
            user_id=user_id,
            is_done=is_done,
            goal_id=goal_id,
            deadline_start=deadline,
            deadline_end=deadline,
        )
        if not success:
            return False, result

        return True, result

    @staticmethod
    def complete_todo(todo_id: int, self_score: int, remark: str, focus_time: Optional[int] = None) -> (bool, str):
        """
        完成Todo任务（业务层：封装「标记完成」的特定逻辑，简化调用）
        :param todo_id: 任务ID
        :param self_score: 完成后的自我评分
        :param remark: 完成后的总结感想
        :param focus_time: 补充的专注时长（可选）
        :return: (bool, 提示字符串)
        """
        # 1. 业务规则校验
        if self_score < 0 or self_score > 10:
            return False, "自我评分需在0-10分之间"
        if not remark.strip():
            return False, "完成总结不能为空"

        # 2. 转发DAO层更新操作（仅更新「完成状态+评分+感想+专注时长」）
        return TodoDAO.update_todo(
            todo_id=todo_id,
            is_done=1,
            self_score=self_score,
            remark=remark,
            focus_time=focus_time
        )

    @staticmethod
    def get_todo_by_project_id(project_id: int):
        """
        根据项目id获取对应的todo信息。
        :param project_id: 项目ID
        :return: (bool, 结果列表/提示字符串)
        """
        return TodoDAO.get_todo_by_project_id(project_id=project_id)